package cn.bocweb.spinning.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.Product1Bean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.ProductDetailActivity;
import cn.bocweb.spinning.view.adapter.ProductListAdapter;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 16/3/9.
 */
public class ProductListFragment extends Fragment {
    ProductListAdapter productListAdapter;
    SwipeRefreshHelper swipeRefreshHelper;
    UserPresenter presenter;
    LoadingDialog dialog;
    int page = 1;
    String userid;
    List<Product1Bean.DataBean> list;
    boolean isLastPage;

    @Bind(R.id.product_main_list)
    GridView productMainList;
    @Bind(R.id.product_main_refresh)
    SwipeRefreshLayout productMainRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_list, container, false);
        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        list = new ArrayList<>();
        dialog = new LoadingDialog(getActivity());
        presenter = new UserPresenterImp();
        productListAdapter = new ProductListAdapter(getActivity(), list);
        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(getActivity());

        userid = dataEntity.getUserid();

        initList();
        dialog.show();
        presenter.getProductList(String.valueOf(page), userid);
    }

    public void onEventMainThread(Product1Bean bean) {
        if (productMainRefresh.isRefreshing()) {
            productMainRefresh.setRefreshing(false);
        }
        dialog.dismiss();
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            productListAdapter.notifyDataSetChanged();
        } else if (bean.getReturn_code() == -201) {
            isLastPage = true;
            DeviceUtil.MyToast(getActivity(), "已经是最后一页");
        } else {
            DeviceUtil.MyToast(getActivity(), bean.getMsg());
        }
    }
    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initList() {
        productMainList.setAdapter(productListAdapter);
        swipeRefreshHelper = new SwipeRefreshHelper(productMainRefresh, productMainList, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                page = 1;
                presenter.getProductList(String.valueOf(page), userid);
                isLastPage = false;
            }

            @Override
            public void onLoad() {
                if (!isLastPage) {
                    page ++;
                    presenter.getProductList(String.valueOf(page), userid);
                } else {
                    DeviceUtil.MyToast(getActivity(), "已经是最后一页");
                }
            }
        });

        productMainList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra(Constant.TO_PRODUCT_DETAIL_ID, list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
